package actions;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import service.VehicleService;
import domain.Vehicle;
/**
 *@author system auto
 */
public class VehicleCreateAction extends Action {
	/**
	 * 车辆service.
	 */
	private VehicleService vehicleService = null;
	/**
	 * 创建车辆.
	 * @param mapping action mapping
	 * @param form action form
	 * @param request http request
	 * @param response http response
	 * @return action forword
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
	    // 验证是否是有效的Token,防止重复提交
	    if (!isTokenValid(request, true)) {
	    	// 重复提交,转到重复提交失败界面
	        return mapping.findForward("error.duplicateSubmit");
	     }
	    try{
	        DynaValidatorForm vehicleForm = (DynaValidatorForm) form;
	        Vehicle vehicle = new Vehicle();
	        this.convertBean(vehicle,vehicleForm);
	        vehicleService.addVehicle(vehicle);
	      }catch(Exception e){
			ActionErrors errors=new ActionErrors();
	        errors.add("errors.data.operate.error", new ActionMessage("errors.data.operate.error",utils.DaoUtil.exceptionToStr(e)));
	        this.addErrors(request, errors);
	        // 设置token 防止重复提交
	        this.saveToken(request);
	        return mapping.getInputForward();
	      }
	      return mapping.findForward("success");
	}
	/**
	 *convertBean
	 */
	 private void convertBean(Vehicle vehicle, DynaValidatorForm vehicleForm)
	 		throws Exception{
	    	String numCode=vehicleForm.getString("numCode");
	        if(numCode!=null&&!numCode.equals("")){
	        	vehicle.setNumCode(numCode);
	        }
	    	String manufactType=vehicleForm.getString("manufactType");
	        if(manufactType!=null&&!manufactType.equals("")){
	        	vehicle.setManufactType(manufactType);
	        }
	    	String carType=vehicleForm.getString("carType");
	        if(carType!=null&&!carType.equals("")){
	        	vehicle.setCarType(carType);
	        }
	    	String registNumber=vehicleForm.getString("registNumber");
	        if(registNumber!=null&&!registNumber.equals("")){
	        	vehicle.setRegistNumber(registNumber);
	        }
	    	String carAge=vehicleForm.getString("carAge");
	        if(carAge!=null&&!carAge.equals("")){
	        	vehicle.setCarAge(Integer.valueOf(carAge));
	        }
	    	String shipGRT=vehicleForm.getString("shipGRT");
	        if(shipGRT!=null&&!shipGRT.equals("")){
	        	vehicle.setShipGRT(Double.valueOf(shipGRT));
	        }
	    	String engineNumber=vehicleForm.getString("engineNumber");
	        if(engineNumber!=null&&!engineNumber.equals("")){
	        	vehicle.setEngineNumber(engineNumber);
	        }
	    	String operaCert=vehicleForm.getString("operaCert");
	        if(operaCert!=null&&!operaCert.equals("")){
	        	vehicle.setOperaCert(operaCert);
	        }
	    	String chargeShipGRT=vehicleForm.getString("chargeShipGRT");
	        if(chargeShipGRT!=null&&!chargeShipGRT.equals("")){
	        	vehicle.setChargeShipGRT(Double.valueOf(chargeShipGRT));
	        }
	    	String buyDate=vehicleForm.getString("buyDate");
	        if(buyDate!=null&&!buyDate.equals("")){
	        	DateFormat DATE_FAMAT_BUYDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
	        	vehicle.setBuyDate(DATE_FAMAT_BUYDATE.parse(buyDate));
	        }
	    	String carName=vehicleForm.getString("carName");
	        if(carName!=null&&!carName.equals("")){
	        	vehicle.setCarName(carName);
	        }
	    	String carNote=vehicleForm.getString("carNote");
	        if(carNote!=null&&!carNote.equals("")){
	        	vehicle.setCarNote(carNote);
	        }
	    	String carStage=vehicleForm.getString("carStage");
	        if(carStage!=null&&!carStage.equals("")){
	        	vehicle.setCarStage(carStage);
	        }
	    	String curPosition=vehicleForm.getString("curPosition");
	        if(curPosition!=null&&!curPosition.equals("")){
	        	vehicle.setCurPosition(curPosition);
	        }
	    	String carStatus=vehicleForm.getString("carStatus");
	        if(carStatus!=null&&!carStatus.equals("")){
	        	vehicle.setCarStatus(carStatus);
	        }
	    	String carVolume=vehicleForm.getString("carVolume");
	        if(carVolume!=null&&!carVolume.equals("")){
	        	vehicle.setCarVolume(Double.valueOf(carVolume));
	        }
	    	String oilRate=vehicleForm.getString("oilRate");
	        if(oilRate!=null&&!oilRate.equals("")){
	        	vehicle.setOilRate(Double.valueOf(oilRate));
	        }
	    	String carBodyNumber=vehicleForm.getString("carBodyNumber");
	        if(carBodyNumber!=null&&!carBodyNumber.equals("")){
	        	vehicle.setCarBodyNumber(carBodyNumber);
	        }
	    	String carColor=vehicleForm.getString("carColor");
	        if(carColor!=null&&!carColor.equals("")){
	        	vehicle.setCarColor(carColor);
	        }
	 }
	/**
     * @param pVehicleService the vehicleService to set
     */
    public void setVehicleService(VehicleService pVehicleService) {
        vehicleService = pVehicleService;
    }
}
